package de.bildung.koblenz.dialogs;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import de.bildung.koblenz.homeswitch.R;
import de.bildung.koblenz.homeswitch.entities.Socket;

public class EditSocketDialog extends AlertDialog.Builder
{
	public interface IEditSocketResult
	{
		public void finish(boolean edited, Socket socket);
	}
	
	private Socket socket;
	private View contentView;
	private IEditSocketResult callback;
	private EditText name;
	
	public EditSocketDialog(Context context, IEditSocketResult dialogResult)
	{
		super(context);
		
		this.callback = dialogResult;
		
		LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		contentView = inflater.inflate(R.layout.dialog_edit_socket, null);
		setView(contentView);
		this.name = (EditText) contentView.findViewById(R.id.dialog_edit_socket_edt_name);
		setPositiveButton(android.R.string.ok, new OnClickListener()
		{
			
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				if(socket == null)
				{
					socket = new Socket();
				}
				socket.setName(name.getText().toString());
				callback.finish(true, socket);
			}
		});
		setNegativeButton(android.R.string.cancel, new OnClickListener()
		{
			
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				callback.finish(false, socket);
			}
		});
		setNeutralButton(R.string.delete, new OnClickListener()
		{
			
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				socket = null;
				callback.finish(true, socket);
			}
		});
	}
	
	public void setSocket(Socket socket)
	{
		this.socket = socket;
		updateView();
	}
	
	private void updateView()
	{
		name.setText(socket.getName());
	}
}
